/*
********************************************************************************
*
*	file : hal
*
********************************************************************************
*/
#include "bsp.h"
#include "sys_task.h" 
#include "pincfg.h"
#include "dataproc.h"
#include "app_update.h" 

/*******************************************************************************
* @brief  ap_flash_read
* \param[in] none
* \retval: none
*******************************************************************************/
uint8_t ap_flash_read(uint32_t addr, uint32_t len, uint8_t *pdat)
{
    uint32_t *p32;

    p32 = (uint32_t *)pdat;
	
	len >>= 2;
	
    while (len--)
    {
        *p32++ = inpw(addr);
		addr += 4;
    }
    
    return 0;
}

/*******************************************************************************
* @brief  ap_flash_write
* \param[in] none
* \retval: none
*******************************************************************************/
uint8_t ap_flash_write(uint32_t addr, uint32_t len, uint8_t *pdat)
{
    uint32_t *p32, index;

    p32 = (uint32_t *)pdat;

    flash_unlock();

    //编程，逆序
    index = len >> 2;
    
    while(index--)
    {
        flash_word_program(addr + index * 4, p32[index]);
    }

    flash_lock();

    return 0;
}

/*******************************************************************************
* @brief  ap_flash_erase
* \param[in] none
* \retval: none
*******************************************************************************/
uint8_t ap_flash_erase(uint32_t addr_start, uint32_t addr_end)
{
	uint32_t addr;
	
    flash_unlock();

	//擦除
	for(addr = addr_start; addr <= addr_end; addr += 2048)
	{
		flash_sector_erase(addr);
	}
	
    flash_lock();
    
    return 0;
}
